import 'package:date_format/date_format.dart';

class FreeDailyLiveListEntity {
  int ret;
  FreeDailyLiveListRows data;
  String msg;

  FreeDailyLiveListEntity({this.ret, this.data, this.msg});

  FreeDailyLiveListEntity.fromJson(Map<String, dynamic> json) {
    ret = json['ret'];
    msg = json['msg'];
    data = json['data'] != null ? new FreeDailyLiveListRows.fromJson(json['data']) : null;
  }
}

class FreeDailyLiveListRows {
  int total;
  List<FreeDailyLiveEntity> rows;

  FreeDailyLiveListRows({this.total, this.rows});

  FreeDailyLiveListRows.fromJson(Map<String, dynamic> json) {
    total = json['total'];
    if (json['rows'] != null) {
      rows = new List<FreeDailyLiveEntity>();
      json['rows'].forEach((v) {
        ///过滤时间为空的数据
        if (v['START_TIME'] != null && v['END_TIME'] != null ) {
          rows.add(new FreeDailyLiveEntity.fromJson(v));
        }
      });
    }
  }
}

/// 每日精品免费直播
class FreeDailyLiveEntity {
  String title;
  String liveUrl;
  String liveToken;
  String vodUrl;
  String vodToken;
  String showTime;
  DateTime startTime;
  DateTime endTime;
  String subject;
  String teacherName;
  String teacherHead;

  ///分组用的日期
  DateTime groupDate;
  String subDesc = '';

  FreeDailyLiveEntity({
    this.title,
    this.liveUrl,
    this.liveToken,
    this.vodUrl,
    this.vodToken,
    this.showTime,
    this.startTime,
    this.endTime,
    this.subject,
    this.teacherName,
    this.teacherHead
  });

  FreeDailyLiveEntity.fromJson(Map<String, dynamic> json) {
    title = json['TITLE'];
    liveUrl = json['STU_URL'];
    liveToken = json['CLIENT_TOKEN'];
    vodUrl = json['HK_URL'];
    vodToken = json['HK_TOKEN'];
    showTime = json['INTERVAL_TIME'];
    //上面已过滤开始时间和结束时间为空的情况
    startTime = DateTime.parse(json['START_TIME']);
    endTime = DateTime.parse(json['END_TIME']);
    groupDate = DateTime(startTime.year, startTime.month, startTime.day);
    subject = json['SUBJECT'];
    teacherName = json['TEACHER_NAME'];
    teacherHead = json['TEACHER_HEAD'];

    subDesc = _formatSubDesc();
  }

  /// 格式化副标题显示
  String _formatSubDesc() {
    return '${formatDate(startTime, ['mm', '.', 'dd', '   ', 'HH', ':', 'nn'])}-${formatDate(endTime, ['HH', ':', 'nn'])}';
  }

  /// 获取直播状态
  String liveStatus() {
    var now = DateTime.now();
    if (now.isBefore(startTime)) {
      return '敬请期待';
    }else if (now.isBefore(endTime)) {
      return '进入直播';
    }else if (vodUrl != null) {
      return '查看回放';
    }else {
      return '直播结束';
    }
  }

}
